/*
 * Copyright (c) 2015-2018 Sfkj Science And Technology Co.,Ltd.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of
 * Sfkj Science And Technology Co.,Ltd.
 * ("Confidential Information").You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Sfkj.
 */
package com.sfkj.walk.command.user;

import com.sfkj.walk.SystemException;
import com.sfkj.walk.aliyun.sms.SendSmsService;
import com.sfkj.walk.api.user.SendCaptchaCommand;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

import static com.sfkj.walk.Contants.CACHE_CAPTCHA_NAME;

@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Service
@Profile("mock_captcha")
public class MockCaptchaCommandHandler {

    private final String CAPTCHA = "1234";

    private final CacheManager cacheManager;

    @CommandHandler
    public void handle(SendCaptchaCommand command) {
        log.info("发送测试验证码:{},{}", command.getPhone(), CAPTCHA);

        //  发送短信验证码
        if (!SendSmsService.TYPES.contains(command.getType())) {
            throw new SystemException("type不支持，type 包含:" + SendSmsService.TYPES.toString());
        }

        cacheManager.getCache(CACHE_CAPTCHA_NAME).put(command.getPhone(), CAPTCHA);
    }

}